package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("C端-地址簿接口")
@RequestMapping("/user/addressBook")
@Slf4j
@RestController
@RequiredArgsConstructor
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> queryAllAddressBook(){
        log.info("查询当前登录用户的所有地址信息：{}");
        List<AddressBook> addressBookList=addressBookService.queryAllAddressBook();
        return Result.success(addressBookList);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefaultAddressBook(@RequestBody AddressBook addressBook){
        log.info("设置默认地址:{}",addressBook);
        addressBookService.setDefaultAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result update(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据Id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> queryAddressBookById(@PathVariable Long id){
        log.info("根据id查询地址:{}",id);
        AddressBook addressBook=addressBookService.queryAddressBookById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据Id删除地址
     * @return
     */
    @ApiOperation("根据Id删除地址")
    @DeleteMapping
    public Result deleteAddressBookById(@RequestParam Long id){
        log.info("根据Id删除地址:{}",id);
        addressBookService.deleteAddressBookById(id);
        return Result.success();

    }

}
